import { WeApp } from '../../../../common/common';
import { config } from '../../../../etc/config';
import httpClient from '../../../../common/http-client';
import { Consignee } from '../../../../model/consignee';
import wxValidate from '../../../../common/wxValidate';
import wxService from '../../../../common/wxService';
import * as wux from '../../../../assets/components/wux';
import utils from '../../../../utils/utils'
import tools from '../../../../common/tools';

const app: WeApp = getApp() as WeApp;
interface UserAddressEditPageData {
	consignee?: Consignee,
	btnDisabled?: boolean,
	btnLoading?: boolean,
	isSelect?: boolean
}

interface UserAddressEditPage extends IPage {

}

class UserAddressEditPage {
	private WxValidate;
	private isInvoice?: boolean;
	// private pages = getCurrentPages();
	// private prevPage = this.pages[this.pages.length - 2];
	public data: UserAddressEditPageData = {
		consignee: {}
	}

	onTapCity() {
		wux.pickerCity.init('city', {
			title: '请选择区域',
			value: [21, 0, 0],
			onChange(p) {
				//console.log(p)
				this.setData({
					'consignee.areas': p.value
				})
			}
		})
	}

	bindKeyInput(e) {
		const key = e.target.dataset.key;
		//const key = e.currentTarget.dataset.key;
		const value = e.detail.value;
		if (e.detail.value) {
			let data = {};
			data[`consignee.${key}`] = value;
			this.setData(data);
		}
	}

	public onLoad(option) {
		this.WxValidate = wxValidate({
			name: {
				required: true,
				minlength: 2,
				maxlength: 10,
			},
			phone: {
				required: true,
				maxlength: 20,
			},
		}, {
				name: {
					required: '请输入收货人',
					minlength: '收货人最少2个字符',
					maxlength: '收货人最多输入10个字符'
				},
				phone: {
					required: '请输入联系电话',
					maxlength: '收货人最多输入20个字符'
				}
			});
		let sData = {};
		if (option.id) {
			var consignee = wx.getStorageSync("addressedit")
			sData['consignee'] = consignee;
		}
		this.isInvoice = option.isInvoice;
		if (option.isSelect != undefined && option.isSelect != null) {
			sData['isSelect'] = option.isSelect;
		}
		this.setData(sData)
	}

	public chooseLocation() {
		wx.chooseLocation({
			success: (res: wx.ChooseLocationResult) => {
				this.setData({
					'consignee.address': res.address
				})
			}
		});
	}

	public submitForm(e) {
		console.log('formId：' + e.detail.formId);
		if (!this.WxValidate.checkForm(e)) {
			const error: { msg: string } = this.WxValidate.errorList[0];
			utils.showErrorToptips(error)
			return false
		}
		const params: Consignee = e.detail.value;
		let consignee = this.data.consignee || {};
		if (!consignee.areas || consignee.areas.length == 0) {
			utils.showErrorToptips({ msg: '还未选择所属区域' })
			return;
		}
		consignee.name = params.name;
		consignee.phone = params.phone;
		consignee.address = params.address;
		const p = tools.clone(consignee);
		p.areas = consignee.areas.join(',')
		utils.setBtnStatus(this);
		httpClient.post<Consignee>(config.consignee.consignee_save, p)
			.then(res => {
				utils.setBtnStatus(this);
				if (res.succeed) {
					// if (consignee.isDefault) {
					// 	app.setCurUserDefaultConsignee(consignee);
					// }
					const isSelect = this.data.isSelect;
					if (isSelect) {
						wx.setStorageSync(config.storageNames.confirmOrderConsignee, consignee);
					}
					wxService.navigateBack(isSelect ? 2 : 1);
				} else {
					wxService.showToast(res.msg);
				}
			}).catch(e => {
				utils.setBtnStatus(this);
			});
	}

}

Page(new UserAddressEditPage());
